package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.dao.SmsFlashPromotionDao;
import com.cloud.mall.sms.dto.product.FlashPromotionProduct;
import com.cloud.mall.sms.dto.product.HomeFlashPromotion;
import com.cloud.mall.sms.input.SmsFlashPromotionInpDTO;
import com.cloud.mall.sms.mapper.SmsFlashPromotionMapper;
import com.cloud.mall.sms.mapper.SmsFlashPromotionSessionMapper;
import com.cloud.mall.sms.model.SmsFlashPromotion;
import com.cloud.mall.sms.model.SmsFlashPromotionExample;
import com.cloud.mall.sms.model.SmsFlashPromotionSession;
import com.cloud.mall.sms.model.SmsFlashPromotionSessionExample;
import com.cloud.mall.sms.out.SmsFlashPromotionOut;
import com.cloud.mall.sms.service.SmsFlashPromotionService;
import com.cloud.mall.util.DateUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class SmsFlashPromotionServiceImpl implements SmsFlashPromotionService {
    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private SmsFlashPromotionDao smsFlashPromotionDao;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody SmsFlashPromotionInpDTO flashPromotionInpDTO) {
        SmsFlashPromotion smsFlashPromotion=MallBeanUtils.dtoToDo(flashPromotionInpDTO, SmsFlashPromotion.class);
        smsFlashPromotion.setCreateTime(new Date());
        int count=flashPromotionMapper.insert(smsFlashPromotion);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse update(Long id,@RequestBody SmsFlashPromotionInpDTO flashPromotionInpDTO) {
        SmsFlashPromotion smsFlashPromotion=MallBeanUtils.dtoToDo(flashPromotionInpDTO, SmsFlashPromotion.class);
        smsFlashPromotion.setId(id);
        int count=flashPromotionMapper.updateByPrimaryKey(smsFlashPromotion);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Transactional
    @Override
    public BaseResponse delete(Long id) {
        flashPromotionMapper.deleteByPrimaryKey(id);
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id, Integer status) {
        SmsFlashPromotion flashPromotion = new SmsFlashPromotion();
        flashPromotion.setId(id);
        flashPromotion.setStatus(status);
        int count=flashPromotionMapper.updateByPrimaryKeySelective(flashPromotion);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<SmsFlashPromotionOut> getItem(Long id) {
        SmsFlashPromotion flashPromotion = flashPromotionMapper.selectByPrimaryKey(id);
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(flashPromotion,SmsFlashPromotionOut.class));
    }

    @Override
    public BaseResponse<CommonPage<SmsFlashPromotionOut>> getItem(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        if (!StringUtils.isEmpty(keyword)) {
            example.createCriteria().andTitleLike("%" + keyword + "%");
        }
        List<SmsFlashPromotion> flashPromotionList=flashPromotionMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(flashPromotionList, SmsFlashPromotionOut.class)));
    }

    @Override
    public BaseResponse<HomeFlashPromotion> getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
        Date now = new Date();
        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
        if (flashPromotion != null) {
            //获取当前秒杀场次
            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
            if (flashPromotionSession != null) {
                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
                if(nextSession!=null){
                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
                }
                //获取秒杀商品
                List<FlashPromotionProduct> flashProductList = smsFlashPromotionDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
                homeFlashPromotion.setProductList(flashProductList);
            }
        }
        return BaseResponse.setResultSuccess(homeFlashPromotion);
    }

    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
        Date currDate = DateUtil.getDate(date);
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        example.createCriteria()
                .andStatusEqualTo(1)
                .andStartDateLessThanOrEqualTo(currDate)
                .andEndDateGreaterThanOrEqualTo(currDate);
        List<SmsFlashPromotion> flashPromotionList = flashPromotionMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(flashPromotionList)) {
            return flashPromotionList.get(0);
        }
        return null;
    }

    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
        Date currTime = DateUtil.getTime(date);
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeLessThanOrEqualTo(currTime)
                .andEndTimeGreaterThanOrEqualTo(currTime);
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }

    //获取下一个场次信息
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeGreaterThan(date);
        sessionExample.setOrderByClause("start_time asc");
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }

}
